#pragma once
#include <atomic>
#include <thread>
#include <functional>
#include <memory>
#include <string>
#include <nlohmann/json.hpp>

extern "C" {
#include <libavformat/avformat.h>
}

#include "decoder/common/subtitle_sync_manager.hpp"
#include "decoder/common/thread_safe_queue.hpp"
namespace WD {

class SubtitleDecoderWorker {
public:
    SubtitleDecoderWorker(int stream_index,
                          AVFormatContext* fmt_ctx,
                          std::shared_ptr<ThreadSafeQueue<AVPacket*>> pkt_queue,
                          std::function<void(const SubtitleInfo&)> subtitle_cb);

    ~SubtitleDecoderWorker();

    void start();
    void stop();

private:
    void decodeLoop();
    SubtitleInfo parseJsonSubtitle(const std::string& json_str);

    int stream_index_;
    AVFormatContext* fmt_ctx_;
    std::shared_ptr<ThreadSafeQueue<AVPacket*>> pkt_queue_;
    std::function<void(const SubtitleInfo&)> subtitle_cb_;

    std::atomic<bool> running_{false};
    std::thread thread_;

    double last_valid_image_utc_ = 0.0;
    uint64_t last_valid_timestamp_ = 0;  // 来自 info.timestamp
};


} // namespace WD
